package com.jlcindia.mvc;


import java.util.Map;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jlcindia.spring.service.StudentService;
import com.jlcindia.to.StudentTO;

@Controller

public class SearchController {
	
	@Autowired
	StudentService studentService;
	
	@RequestMapping("/")
	public String showSearch(Map model){
		StudentMVC stud = new StudentMVC();
		model.put("student",stud);
		return "index";
	}
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	public String searchStudent(@ModelAttribute("student") StudentMVC stud, BindingResult result,Httpsession session){
		StudentTO sto = studentService.getStudentBySid(""+stud.getSid());
		if(sto==null){
			session.removeAttribute("STUD");
			result.rejectValue("error","error.sid",null,"NO DETAILS FOUND WITH ID " + stud.getSid());
		}else{
			session.setAttribute("STUD",sto);
		}
		return "index";
	}
}
